package com.cda.mall.controller;


import com.cda.mall.entity.Users;
import com.cda.mall.service.IUsersService;
import com.cda.mall.vo.ResultVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 用户  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/user")
@Api(value = "提供用户的登录和注册接口",tags = "用户管理")
//后端解决跨域问题（实际是对response进行设置）
@CrossOrigin
public class UsersController {

    @Resource
    private IUsersService usersService;

    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "username",value = "用户登录账号",required = true),
            @ApiImplicitParam(dataType = "string",name = "password",value = "用户登录密码",required = true)
    })
    @GetMapping("/login")
//    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ResultVO login(@RequestParam("username") String name,
                          @RequestParam("password") String pwd){
        return usersService.checkLogin(name,pwd);
    }

    @ApiOperation("用户注册接口")
//    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "username",value = "用户注册账号",required = true),
            @ApiImplicitParam(dataType = "string",name = "password",value = "用户注册密码",required = true)
    })
    @PostMapping("/regist")
    public ResultVO regist(@RequestBody Users user){
        return usersService.userRegister(user.getUsername(), user.getPassword());
    }
}

